<template>
  <div class="app-container">

    <div>
      <el-input v-model="pageDto.where.name" placeholder="请输入租户名" style="width: 250px;margin-left: 20px;"></el-input>
      <el-input v-model="pageDto.where.accessKey" placeholder="请输入租户Key"
        style="width: 250px;margin-left: 20px;"></el-input>
      <el-button type="primary" icon="el-icon-search" style="margin-left: 20px;"
        @click="pageDto.now = 1; search()">搜索</el-button>
      <el-button type="danger" icon="el-icon-refresh" @click="reset">清空</el-button>
    </div>

    <!-- 页面表格渲染 -->
    <div style="margin-top: 20px;">
      <el-table :data="tenantList" style="width: 100%;margin-bottom: 20px;margin-bottom: 20px;"
        :header-cell-style="{ background: '#F7F7F8', color: '#18191B', 'text-align': 'center' }">
        <el-table-column prop="name" label="租户名" align="center"> </el-table-column>
        <el-table-column prop="accessKey" label="租户Key" align="center"></el-table-column>
        <el-table-column prop="companyName" label="公司名称" align="center"></el-table-column>
        <el-table-column prop="companyCode" label="执照编号" align="center"></el-table-column>
        <el-table-column prop="phone" label="手机号" align="center"></el-table-column>
        <el-table-column label="是否可用" align="center">
          <!-- 自定义渲染 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.enable == 1" key="可用" type="success">
              {{ '可用' }}
            </el-tag>
            <el-tag v-if="scope.row.enable == 2" key="不可用" type="danger">
              {{ '不可用' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="description" label="描述" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <!-- 自定义渲染 -->
          <template slot-scope="scope">
            <el-tag v-if="scope.row.auditStatus == 0" key="待审核">
              {{ '待审核' }}
            </el-tag>
            <el-tag v-if="scope.row.auditStatus == 1" key="通过" type="success">
              {{ '通过' }}
            </el-tag>
            <el-tag v-if="scope.row.auditStatus == 2" key="不通过" type="danger">
              {{ '不通过' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-link v-if="scope.row.enable == 2" @click="open(scope.row)" type="primary" :underline="false">启用</el-link>
            <el-link v-if="scope.row.enable == 1" @click="close(scope.row)" type="danger" :underline="false">禁用</el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页组件 -->
      <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageDto.now"
        :page-sizes="[5, 10, 50, 100]" :page-size="pageDto.size" layout="total, sizes, prev, pager, next, jumper"
        :total="total" style="text-align: center;">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import { page, instruction } from "@/api/tenantManager"
export default {
  data() {
    return {
      pageDto: {
        now: 1,
        size: 5,
        where: {}
      },
      pages: 0,
      total: 0,
      tenantList: [],
      tenant: {},
      flag: false
    }
  },
  methods: {
    search() {
      page(this.pageDto).then((res) => {
        this.tenantList = res.list;
        this.pages = res.pages;
        this.total = res.total;
      });
    },

    handleSizeChange(size) {
      this.pageDto.size = size;
      this.search();
    },

    handleCurrentChange(now) {
      this.pageDto.now = now;
      this.search();
    },

    //格式日期
    dateFormat(row, column, cellValue, index) {
      const daterc = row[column.property]
      if (daterc) {
        if (daterc.indexOf('T') === -1) return daterc
        const arr = daterc.split('T')
        const d = arr[0]
        const darr = d.split('-')
        const t = arr[1]
        const tarr = t.split('.000')
        const marr = tarr[0].split(':')
        const dd = darr[0] + '-' + darr[1] + '-' + darr[2] + ' ' + parseInt(marr[0]) + ':' + marr[1] + ':' + marr[2].substring(0, 2)
        return dd
      }
    },

    reset() {
      this.pageDto.where = {}
    },

    edit(data) {
      this.tenant = data
      if (this.tenant.enable == 1) {
        this.tenant.enable = 2
      } else {
        this.tenant.enable = 1
      }
      instruction(this.tenant).then((res) => {
        if (res.code == 0) {
          this.flag = true
        } else {
          this.flag = false
        }
      })
    },

    open(data) {
      this.$confirm('您确定启用该用户？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.edit(data)
        if (this.flag) {
          this.$message({
            type: 'success',
            message: '启用成功!'
          });
        } else {
          this.$message({
            type: 'error',
            message: '启用失败!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消启用'
        });
      });
      this.search()
      this.flag = false
    },

    close(data) {
      this.$confirm('您确定禁用该用户？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.edit(data)
        if (this.flag) {
          this.$message({
            type: 'success',
            message: '禁用成功!'
          });
        } else {
          this.$message({
            type: 'error',
            message: '禁用失败!'
          });
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消禁用'
        });
      });
      this.search()
      this.flag = false
    }


  },
  created() {
    this.search()
  }
}
</script>

<style></style>
